import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { ConfigModule, ConfigService } from '@nestjs/config'

import { AppController } from './app.controller'
import { AppService } from './app.service'

import { getEnv } from './config'
import { AuthModule, UserModule } from './modules'
import { APP_GUARD } from '@nestjs/core'
import { JwtAuthGuard } from './common/guards/auth/auth.guard'
import { PostModule } from './modules/post/post.module'

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (service: ConfigService) => service.get('DATABASE_CONFIG')
    }),
    ConfigModule.forRoot({
      envFilePath: ['.env', '.env.dev', '.env.prod'],
      isGlobal: true,
      load: [getEnv]
    }),
    AuthModule,
    UserModule,
    PostModule
  ],
  controllers: [AppController],
  providers: [
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard
    },
    AppService
  ]
})
export class AppModule {}
